package com.fingard.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
/**
 * DEFLATE压缩方法
 * @author Lock
 */
public class DEHelper {

	/** DEFLATE解压方法 */
	public static byte[] uncompress(byte[] inputByte) throws IOException {
		int len = 0;
		Inflater infl = new Inflater();
		infl.setInput(inputByte);
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] outByte = new byte[1024];
		try {
			while (!infl.finished()) {
				// 解压缩并将解压缩后的内容输出到字节输出流bos中
				len = infl.inflate(outByte);
				if (len == 0) {
					break;
				}
				bos.write(outByte, 0, len);
			}
			infl.end();
		} catch (Exception e) {
			//
		} finally {
			bos.close();
		}
		return bos.toByteArray();
	}
	
	/**压缩方法 */
	public static byte[] compress(byte[] inputByte) throws IOException {
	    int compressedDataLength = 0;
	    Deflater compresser = new Deflater();
	    compresser.setInput(inputByte);
	    compresser.finish();
	    ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length);
	    byte[] result = new byte[1024];
	    try {
	      while (!compresser.finished()) {
	        compressedDataLength = compresser.deflate(result);
	        o.write(result, 0, compressedDataLength);
	      }
	    } finally {
	      o.close();
	    }
	    compresser.end();
	    return o.toByteArray();
    }
}
